package com.zzmm.socket.qq.common;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class DataBase {

    public static List<String> listAllUsername() {
        return new ArrayList<>(USERS.keySet());
    }

    public static User getUserByUsername(String username) {
        return USERS.get(username);
    }

    public static void saveOfflineMessageByUsernameIfExists(Message message) {
        String receiver = message.getReceiver();
        if (!USERS.containsKey(receiver)) {
            ConsoleUtil.printf("【%】不存在，消息【%s】无法被接收\n", receiver, message);
            return;
        }
        Queue<Message> messages = OFFLINE_MESSAGE.computeIfAbsent(receiver, key -> new LinkedList<>());
        messages.add(message);
        ConsoleUtil.printf("用户【%s】的离线消息存储成功：%s\n", receiver, message);
    }

    public static Queue<Message> listOfflineMessageByUsername(String username) {
        return OFFLINE_MESSAGE.get(username);
    }

    private static final Map<String, User> USERS = new HashMap<String, User>() {{
        put("root", new User("root", "root"));
        put("100", new User("100", "123456"));
        put("悟空", new User("悟空", "123456"));
        put("八戒", new User("八戒", "123456"));
        put("沙僧", new User("沙僧", "123456"));
        put("唐僧", new User("唐僧", "123456"));
    }};

    private static final Map<String, Queue<Message>> OFFLINE_MESSAGE = new ConcurrentHashMap<>();


}
